<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.contrib.formtools.wizard.views &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../../index.html" />
    <link rel="up" title="django" href="../../../../django.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-formtools-wizard-views">
            
  <h1>Source code for django.contrib.formtools.wizard.views</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">redirect</span>
<span class="kn">from</span> <span class="nn">django.core.urlresolvers</span> <span class="kn">import</span> <span class="n">reverse</span>
<span class="kn">from</span> <span class="nn">django.forms</span> <span class="kn">import</span> <span class="n">formsets</span><span class="p">,</span> <span class="n">ValidationError</span>
<span class="kn">from</span> <span class="nn">django.views.generic</span> <span class="kn">import</span> <span class="n">TemplateView</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="kn">import</span> <span class="n">classonlymethod</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>

<span class="kn">from</span> <span class="nn">django.contrib.formtools.wizard.storage</span> <span class="kn">import</span> <span class="n">get_storage</span>
<span class="kn">from</span> <span class="nn">django.contrib.formtools.wizard.storage.exceptions</span> <span class="kn">import</span> <span class="n">NoFileStorageConfigured</span>
<span class="kn">from</span> <span class="nn">django.contrib.formtools.wizard.forms</span> <span class="kn">import</span> <span class="n">ManagementForm</span>


<span class="k">def</span> <span class="nf">normalize_name</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts camel-case style names into underscore separated words. Example::</span>

<span class="sd">        &gt;&gt;&gt; normalize_name(&#39;oneTwoThree&#39;)</span>
<span class="sd">        &#39;one_two_three&#39;</span>
<span class="sd">        &gt;&gt;&gt; normalize_name(&#39;FourFiveSix&#39;)</span>
<span class="sd">        &#39;four_five_six&#39;</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">new</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;(((?&lt;=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))&#39;</span><span class="p">,</span> <span class="s">&#39;_</span><span class="se">\\</span><span class="s">1&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">new</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s">&#39;_&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">StepsHelper</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wizard</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span> <span class="o">=</span> <span class="n">wizard</span>

    <span class="k">def</span> <span class="nf">__dir__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">all</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;&lt;StepsHelper for </span><span class="si">%s</span><span class="s"> (steps: </span><span class="si">%s</span><span class="s">)&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">all</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">all</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the names of all steps/forms.&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">())</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the total number of steps/forms in this the wizard.&quot;</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">all</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">current</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the current step. If no current step is stored in the</span>
<span class="sd">        storage backend, the first step will be returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">first</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">first</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the name of the first step.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">all</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">last</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the name of the last step.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">all</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the next step.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span><span class="o">.</span><span class="n">get_next_step</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">prev</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the previous step.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span><span class="o">.</span><span class="n">get_prev_step</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns the index for the current step.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_wizard</span><span class="o">.</span><span class="n">get_step_index</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">step0</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">step1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>


<div class="viewcode-block" id="WizardView"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView">[docs]</a><span class="k">class</span> <span class="nc">WizardView</span><span class="p">(</span><span class="n">TemplateView</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    The WizardView is used to create multi-page forms and handles all the</span>
<span class="sd">    storage and validation stuff. The wizard is based on Django&#39;s generic</span>
<span class="sd">    class based views.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">storage_name</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">form_list</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">initial_dict</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">instance_dict</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">condition_dict</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&#39;formtools/wizard/wizard_form.html&#39;</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;&lt;</span><span class="si">%s</span><span class="s">: forms: </span><span class="si">%s</span><span class="s">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_list</span><span class="p">)</span>

    <span class="nd">@classonlymethod</span>
<div class="viewcode-block" id="WizardView.as_view"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.as_view">[docs]</a>    <span class="k">def</span> <span class="nf">as_view</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method is used within urls.py to create unique wizardview</span>
<span class="sd">        instances for every request. We need to override this method because</span>
<span class="sd">        we add some kwargs which are needed to make the wizardview usable.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">initkwargs</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">get_initkwargs</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">WizardView</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="o">**</span><span class="n">initkwargs</span><span class="p">)</span>
</div>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">get_initkwargs</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">form_list</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">initial_dict</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
            <span class="n">instance_dict</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">condition_dict</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Creates a dict with all needed parameters for the form wizard instances.</span>

<span class="sd">        * `form_list` - is a list of forms. The list entries can be single form</span>
<span class="sd">          classes or tuples of (`step_name`, `form_class`). If you pass a list</span>
<span class="sd">          of forms, the wizardview will convert the class list to</span>
<span class="sd">          (`zero_based_counter`, `form_class`). This is needed to access the</span>
<span class="sd">          form for a specific step.</span>
<span class="sd">        * `initial_dict` - contains a dictionary of initial data dictionaries.</span>
<span class="sd">          The key should be equal to the `step_name` in the `form_list` (or</span>
<span class="sd">          the str of the zero based counter - if no step_names added in the</span>
<span class="sd">          `form_list`)</span>
<span class="sd">        * `instance_dict` - contains a dictionary whose values are model</span>
<span class="sd">          instances if the step is based on a ``ModelForm`` and querysets if</span>
<span class="sd">          the step is based on a ``ModelFormSet``. The key should be equal to</span>
<span class="sd">          the `step_name` in the `form_list`. Same rules as for `initial_dict`</span>
<span class="sd">          apply.</span>
<span class="sd">        * `condition_dict` - contains a dictionary of boolean values or</span>
<span class="sd">          callables. If the value of for a specific `step_name` is callable it</span>
<span class="sd">          will be called with the wizardview instance as the only argument.</span>
<span class="sd">          If the return value is true, the step&#39;s form will be used.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
            <span class="s">&#39;initial_dict&#39;</span><span class="p">:</span> <span class="n">initial_dict</span> <span class="ow">or</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;initial_dict&#39;</span><span class="p">,</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="s">&#39;initial_dict&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span> <span class="ow">or</span> <span class="p">{},</span>
            <span class="s">&#39;instance_dict&#39;</span><span class="p">:</span> <span class="n">instance_dict</span> <span class="ow">or</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;instance_dict&#39;</span><span class="p">,</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="s">&#39;instance_dict&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span> <span class="ow">or</span> <span class="p">{},</span>
            <span class="s">&#39;condition_dict&#39;</span><span class="p">:</span> <span class="n">condition_dict</span> <span class="ow">or</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;condition_dict&#39;</span><span class="p">,</span>
                <span class="nb">getattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="s">&#39;condition_dict&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="p">})</span>

        <span class="n">form_list</span> <span class="o">=</span> <span class="n">form_list</span> <span class="ow">or</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;form_list&#39;</span><span class="p">,</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="s">&#39;form_list&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span> <span class="ow">or</span> <span class="p">[]</span>

        <span class="n">computed_form_list</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>

        <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">form_list</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">,</span> <span class="s">&#39;at least one form is needed&#39;</span>

        <span class="c"># walk through the passed form list</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">form</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">form_list</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
                <span class="c"># if the element is a tuple, add the tuple to the new created</span>
                <span class="c"># sorted dictionary.</span>
                <span class="n">computed_form_list</span><span class="p">[</span><span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">form</span><span class="p">[</span><span class="mi">0</span><span class="p">])]</span> <span class="o">=</span> <span class="n">form</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># if not, add the form with a zero based counter as unicode</span>
                <span class="n">computed_form_list</span><span class="p">[</span><span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span> <span class="o">=</span> <span class="n">form</span>

        <span class="c"># walk through the new created list of forms</span>
        <span class="k">for</span> <span class="n">form</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">itervalues</span><span class="p">(</span><span class="n">computed_form_list</span><span class="p">):</span>
            <span class="k">if</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="n">formsets</span><span class="o">.</span><span class="n">BaseFormSet</span><span class="p">):</span>
                <span class="c"># if the element is based on BaseFormSet (FormSet/ModelFormSet)</span>
                <span class="c"># we need to override the form variable.</span>
                <span class="n">form</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">form</span>
            <span class="c"># check if any form contains a FileField, if yes, we need a</span>
            <span class="c"># file_storage added to the wizardview (by subclassing).</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">itervalues</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">base_fields</span><span class="p">):</span>
                <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">forms</span><span class="o">.</span><span class="n">FileField</span><span class="p">)</span> <span class="ow">and</span>
                        <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="s">&#39;file_storage&#39;</span><span class="p">)):</span>
                    <span class="k">raise</span> <span class="n">NoFileStorageConfigured</span><span class="p">(</span>
                        <span class="s">&quot;You need to define &#39;file_storage&#39; in your &quot;</span>
                        <span class="s">&quot;wizard view in order to handle file uploads.&quot;</span><span class="p">)</span>

        <span class="c"># build the kwargs for the wizardview instances</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;form_list&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">computed_form_list</span>
        <span class="k">return</span> <span class="n">kwargs</span>

<div class="viewcode-block" id="WizardView.get_prefix"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_prefix">[docs]</a>    <span class="k">def</span> <span class="nf">get_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c"># TODO: Add some kind of unique id to prefix</span>
        <span class="k">return</span> <span class="n">normalize_name</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">get_form_list</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method returns a form_list based on the initial form list but</span>
<span class="sd">        checks if there is a condition method/value in the condition_list.</span>
<span class="sd">        If an entry exists in the condition list, it will call/read the value</span>
<span class="sd">        and respect the result. (True means add the form, False means ignore</span>
<span class="sd">        the form)</span>

<span class="sd">        The form_list is always generated on the fly because condition methods</span>
<span class="sd">        could use data from other (maybe previous forms).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">form_list</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">form_key</span><span class="p">,</span> <span class="n">form_class</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">form_list</span><span class="p">):</span>
            <span class="c"># try to fetch the value from condition list, by default, the form</span>
            <span class="c"># gets passed to the new list.</span>
            <span class="n">condition</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">condition_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">form_key</span><span class="p">,</span> <span class="bp">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">condition</span><span class="p">):</span>
                <span class="c"># call the value if needed, passes the current instance.</span>
                <span class="n">condition</span> <span class="o">=</span> <span class="n">condition</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">condition</span><span class="p">:</span>
                <span class="n">form_list</span><span class="p">[</span><span class="n">form_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">form_class</span>
        <span class="k">return</span> <span class="n">form_list</span>

    <span class="k">def</span> <span class="nf">dispatch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method gets called by the routing engine. The first argument is</span>
<span class="sd">        `request` which contains a `HttpRequest` instance.</span>
<span class="sd">        The request is stored in `self.request` for later use. The storage</span>
<span class="sd">        instance is stored in `self.storage`.</span>

<span class="sd">        After processing the request using the `dispatch` method, the</span>
<span class="sd">        response gets updated by the storage engine (for example add cookies).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># add the storage engine to the current wizardview instance</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_prefix</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span> <span class="o">=</span> <span class="n">get_storage</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">storage_name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span>
            <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&#39;file_storage&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">steps</span> <span class="o">=</span> <span class="n">StepsHelper</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">response</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">WizardView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">dispatch</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c"># update the response (e.g. adding cookies)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">update_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">response</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method handles GET requests.</span>

<span class="sd">        If a GET request reaches this point, the wizard assumes that the user</span>
<span class="sd">        just starts at the first step or wants to restart the process.</span>
<span class="sd">        The data of the wizard will be resetted before rendering the first step.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>

        <span class="c"># reset the current step to the first step.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">first</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method handles POST requests.</span>

<span class="sd">        The wizard will render either the current step (if form validation</span>
<span class="sd">        wasn&#39;t successful), the next step (if the current step was stored</span>
<span class="sd">        successful) or the done view (if no more steps are available)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># Look for a wizard_goto_step element in the posted data which</span>
        <span class="c"># contains a valid step name. If one was found, render the requested</span>
        <span class="c"># form. (This makes stepping back a lot easier).</span>
        <span class="n">wizard_goto_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;wizard_goto_step&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">wizard_goto_step</span> <span class="ow">and</span> <span class="n">wizard_goto_step</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">():</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_goto_step</span><span class="p">(</span><span class="n">wizard_goto_step</span><span class="p">)</span>

        <span class="c"># Check if form was refreshed</span>
        <span class="n">management_form</span> <span class="o">=</span> <span class="n">ManagementForm</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">management_form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s">&#39;ManagementForm data is missing or has been tampered.&#39;</span><span class="p">),</span>
                <span class="n">code</span><span class="o">=</span><span class="s">&#39;missing_management_form&#39;</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="n">form_current_step</span> <span class="o">=</span> <span class="n">management_form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;current_step&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">form_current_step</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span> <span class="ow">and</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">):</span>
            <span class="c"># form refreshed, change current step</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">form_current_step</span>

        <span class="c"># get the form for the current step</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">FILES</span><span class="p">)</span>

        <span class="c"># and try to validate</span>
        <span class="k">if</span> <span class="n">form</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
            <span class="c"># if the form is valid, store the cleaned data and files.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">set_step_data</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">process_step</span><span class="p">(</span><span class="n">form</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">set_step_files</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">process_step_files</span><span class="p">(</span><span class="n">form</span><span class="p">))</span>

            <span class="c"># check if the current step is the last step</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">last</span><span class="p">:</span>
                <span class="c"># no more steps, render done view</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_done</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># proceed to the next step</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_next_step</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">render_next_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method gets called when the next step/form should be rendered.</span>
<span class="sd">        `form` contains the last/current form.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c"># get the form instance based on the data from the storage backend</span>
        <span class="c"># (if available).</span>
        <span class="n">next_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">next</span>
        <span class="n">new_form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">next_step</span><span class="p">,</span>
            <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_data</span><span class="p">(</span><span class="n">next_step</span><span class="p">),</span>
            <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_files</span><span class="p">(</span><span class="n">next_step</span><span class="p">))</span>

        <span class="c"># change the stored current step</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">next_step</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">new_form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<div class="viewcode-block" id="WizardView.render_goto_step"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.render_goto_step">[docs]</a>    <span class="k">def</span> <span class="nf">render_goto_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">goto_step</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method gets called when the current step has to be changed.</span>
<span class="sd">        `goto_step` contains the requested step to go to.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">goto_step</span>
        <span class="n">form</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span>
            <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_data</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">),</span>
            <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_files</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
</div>
    <span class="k">def</span> <span class="nf">render_done</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method gets called when all forms passed. The method should also</span>
<span class="sd">        re-validate all steps to prevent manipulation. If any form fails to</span>
<span class="sd">        validate, `render_revalidation_failure` should get called.</span>
<span class="sd">        If everything is fine call `done`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">final_forms</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
        <span class="c"># walk through the form list and try to validate the data again.</span>
        <span class="k">for</span> <span class="n">form_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">():</span>
            <span class="n">form_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">step</span><span class="o">=</span><span class="n">form_key</span><span class="p">,</span>
                <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_data</span><span class="p">(</span><span class="n">form_key</span><span class="p">),</span>
                <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_files</span><span class="p">(</span><span class="n">form_key</span><span class="p">))</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">form_obj</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_revalidation_failure</span><span class="p">(</span><span class="n">form_key</span><span class="p">,</span> <span class="n">form_obj</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="n">final_forms</span><span class="p">[</span><span class="n">form_key</span><span class="p">]</span> <span class="o">=</span> <span class="n">form_obj</span>

        <span class="c"># render the done view and reset the wizard before returning the</span>
        <span class="c"># response. This is needed to prevent from rendering done with the</span>
        <span class="c"># same data twice.</span>
        <span class="n">done_response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">done</span><span class="p">(</span><span class="n">final_forms</span><span class="o">.</span><span class="n">values</span><span class="p">(),</span> <span class="n">form_dict</span><span class="o">=</span><span class="n">final_forms</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">done_response</span>

<div class="viewcode-block" id="WizardView.get_form_prefix"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form_prefix">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the prefix which will be used when calling the actual form for</span>
<span class="sd">        the given step. `step` contains the step-name, `form` the form which</span>
<span class="sd">        will be called with the returned prefix.</span>

<span class="sd">        If no step is given, the form_prefix will determine the current step</span>
<span class="sd">        automatically.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">step</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.get_form_initial"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form_initial">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_initial</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a dictionary which will be passed to the form for `step`</span>
<span class="sd">        as `initial`. If no initial data was provided while initializing the</span>
<span class="sd">        form wizard, an empty dictionary will be returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">initial_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="p">{})</span>
</div>
<div class="viewcode-block" id="WizardView.get_form_instance"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form_instance">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_instance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns an object which will be passed to the form for `step`</span>
<span class="sd">        as `instance`. If no instance object was provided while initializing</span>
<span class="sd">        the form wizard, None will be returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.get_form_kwargs"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form_kwargs">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_kwargs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the keyword arguments for instantiating the form</span>
<span class="sd">        (or formset) on the given step.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">{}</span>
</div>
<div class="viewcode-block" id="WizardView.get_form"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form">[docs]</a>    <span class="k">def</span> <span class="nf">get_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">files</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs the form for a given `step`. If no `step` is defined, the</span>
<span class="sd">        current step will be determined automatically.</span>

<span class="sd">        The form will be initialized using the `data` argument to prefill the</span>
<span class="sd">        new form. If needed, instance or queryset (for `ModelForm` or</span>
<span class="sd">        `ModelFormSet`) will be added too.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">step</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span>
        <span class="n">form_class</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_list</span><span class="p">[</span><span class="n">step</span><span class="p">]</span>
        <span class="c"># prepare the kwargs for the form instance.</span>
        <span class="n">kwargs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_kwargs</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
            <span class="s">&#39;data&#39;</span><span class="p">:</span> <span class="n">data</span><span class="p">,</span>
            <span class="s">&#39;files&#39;</span><span class="p">:</span> <span class="n">files</span><span class="p">,</span>
            <span class="s">&#39;prefix&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_prefix</span><span class="p">(</span><span class="n">step</span><span class="p">,</span> <span class="n">form_class</span><span class="p">),</span>
            <span class="s">&#39;initial&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_initial</span><span class="p">(</span><span class="n">step</span><span class="p">),</span>
        <span class="p">})</span>
        <span class="k">if</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">form_class</span><span class="p">,</span> <span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">ModelForm</span><span class="p">,</span> <span class="n">forms</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">BaseInlineFormSet</span><span class="p">)):</span>
            <span class="c"># If the form is based on ModelForm or InlineFormSet,</span>
            <span class="c"># add instance if available and not previously set.</span>
            <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;instance&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_instance</span><span class="p">(</span><span class="n">step</span><span class="p">))</span>
        <span class="k">elif</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">form_class</span><span class="p">,</span> <span class="n">forms</span><span class="o">.</span><span class="n">models</span><span class="o">.</span><span class="n">BaseModelFormSet</span><span class="p">):</span>
            <span class="c"># If the form is based on ModelFormSet, add queryset if available</span>
            <span class="c"># and not previous set.</span>
            <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s">&#39;queryset&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_instance</span><span class="p">(</span><span class="n">step</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">form_class</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.process_step"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.process_step">[docs]</a>    <span class="k">def</span> <span class="nf">process_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method is used to postprocess the form data. By default, it</span>
<span class="sd">        returns the raw `form.data` dictionary.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_step_data</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.process_step_files"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.process_step_files">[docs]</a>    <span class="k">def</span> <span class="nf">process_step_files</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method is used to postprocess the form files. By default, it</span>
<span class="sd">        returns the raw `form.files` dictionary.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_step_files</span><span class="p">(</span><span class="n">form</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.render_revalidation_failure"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.render_revalidation_failure">[docs]</a>    <span class="k">def</span> <span class="nf">render_revalidation_failure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Gets called when a form doesn&#39;t validate when rendering the done</span>
<span class="sd">        view. By default, it changes the current step to failing forms step</span>
<span class="sd">        and renders the form.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">step</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.get_form_step_data"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form_step_data">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_step_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Is used to return the raw form data. You may use this method to</span>
<span class="sd">        manipulate the data.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">data</span>
</div>
<div class="viewcode-block" id="WizardView.get_form_step_files"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_form_step_files">[docs]</a>    <span class="k">def</span> <span class="nf">get_form_step_files</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Is used to return the raw form files. You may use this method to</span>
<span class="sd">        manipulate the data.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">files</span>
</div>
<div class="viewcode-block" id="WizardView.get_all_cleaned_data"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_all_cleaned_data">[docs]</a>    <span class="k">def</span> <span class="nf">get_all_cleaned_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a merged dictionary of all step cleaned_data dictionaries.</span>
<span class="sd">        If a step contains a `FormSet`, the key will be prefixed with</span>
<span class="sd">        &#39;formset-&#39; and contain a list of the formset cleaned_data dictionaries.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cleaned_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">form_key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">():</span>
            <span class="n">form_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span>
                <span class="n">step</span><span class="o">=</span><span class="n">form_key</span><span class="p">,</span>
                <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_data</span><span class="p">(</span><span class="n">form_key</span><span class="p">),</span>
                <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_files</span><span class="p">(</span><span class="n">form_key</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">form_obj</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">form_obj</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
                    <span class="n">cleaned_data</span><span class="o">.</span><span class="n">update</span><span class="p">({</span>
                        <span class="s">&#39;formset-</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">form_key</span><span class="p">:</span> <span class="n">form_obj</span><span class="o">.</span><span class="n">cleaned_data</span>
                    <span class="p">})</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">cleaned_data</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">form_obj</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">cleaned_data</span>
</div>
<div class="viewcode-block" id="WizardView.get_cleaned_data_for_step"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_cleaned_data_for_step">[docs]</a>    <span class="k">def</span> <span class="nf">get_cleaned_data_for_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the cleaned data for a given `step`. Before returning the</span>
<span class="sd">        cleaned data, the stored values are revalidated through the form.</span>
<span class="sd">        If the data doesn&#39;t validate, None will be returned.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">step</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_list</span><span class="p">:</span>
            <span class="n">form_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">step</span><span class="o">=</span><span class="n">step</span><span class="p">,</span>
                <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_data</span><span class="p">(</span><span class="n">step</span><span class="p">),</span>
                <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_files</span><span class="p">(</span><span class="n">step</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">form_obj</span><span class="o">.</span><span class="n">is_valid</span><span class="p">():</span>
                <span class="k">return</span> <span class="n">form_obj</span><span class="o">.</span><span class="n">cleaned_data</span>
        <span class="k">return</span> <span class="bp">None</span>
</div>
    <span class="k">def</span> <span class="nf">get_next_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the next step after the given `step`. If no more steps are</span>
<span class="sd">        available, None will be returned. If the `step` argument is None, the</span>
<span class="sd">        current step will be determined automatically.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">step</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span>
        <span class="n">form_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">()</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">form_list</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="n">key</span> <span class="o">=</span> <span class="n">keys</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">step</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">key</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">keys</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_prev_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the previous step before the given `step`. If there are no</span>
<span class="sd">        steps available, None will be returned. If the `step` argument is</span>
<span class="sd">        None, the current step will be determined automatically.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">step</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span>
        <span class="n">form_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">()</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">form_list</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
        <span class="n">key</span> <span class="o">=</span> <span class="n">keys</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">step</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="n">key</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">keys</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_step_index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the index for the given `step` name. If no step is given,</span>
<span class="sd">        the current step will be used to get the index.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">step</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">()</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>

<div class="viewcode-block" id="WizardView.get_context_data"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.get_context_data">[docs]</a>    <span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns the template context for a step. You can overwrite this method</span>
<span class="sd">        to add more data for all or some steps. This method returns a</span>
<span class="sd">        dictionary containing the rendered form step. Available template</span>
<span class="sd">        context variables are:</span>

<span class="sd">         * all extra data stored in the storage backend</span>
<span class="sd">         * `wizard` - a dictionary representation of the wizard instance</span>

<span class="sd">        Example:</span>

<span class="sd">        .. code-block:: python</span>

<span class="sd">            class MyWizard(WizardView):</span>
<span class="sd">                def get_context_data(self, form, **kwargs):</span>
<span class="sd">                    context = super(MyWizard, self).get_context_data(form=form, **kwargs)</span>
<span class="sd">                    if self.steps.current == &#39;my_step_name&#39;:</span>
<span class="sd">                        context.update({&#39;another_var&#39;: True})</span>
<span class="sd">                    return context</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">WizardView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">context</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">extra_data</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s">&#39;wizard&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;form&#39;</span><span class="p">:</span> <span class="n">form</span><span class="p">,</span>
            <span class="s">&#39;steps&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="p">,</span>
            <span class="s">&#39;management_form&#39;</span><span class="p">:</span> <span class="n">ManagementForm</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">prefix</span><span class="p">,</span> <span class="n">initial</span><span class="o">=</span><span class="p">{</span>
                <span class="s">&#39;current_step&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">,</span>
            <span class="p">}),</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">context</span>
</div>
<div class="viewcode-block" id="WizardView.render"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.render">[docs]</a>    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a ``HttpResponse`` containing all needed context data.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">form</span> <span class="o">=</span> <span class="n">form</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">()</span>
        <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_to_response</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="WizardView.done"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.WizardView.done">[docs]</a>    <span class="k">def</span> <span class="nf">done</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form_list</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method must be overridden by a subclass to process to form data</span>
<span class="sd">        after processing all steps.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s">&quot;Your </span><span class="si">%s</span><span class="s"> class has not defined a done() &quot;</span>
            <span class="s">&quot;method, which is required.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span><span class="p">)</span>

</div></div>
<div class="viewcode-block" id="SessionWizardView"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.SessionWizardView">[docs]</a><span class="k">class</span> <span class="nc">SessionWizardView</span><span class="p">(</span><span class="n">WizardView</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A WizardView with pre-configured SessionStorage backend.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">storage_name</span> <span class="o">=</span> <span class="s">&#39;django.contrib.formtools.wizard.storage.session.SessionStorage&#39;</span>

</div>
<div class="viewcode-block" id="CookieWizardView"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.CookieWizardView">[docs]</a><span class="k">class</span> <span class="nc">CookieWizardView</span><span class="p">(</span><span class="n">WizardView</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A WizardView with pre-configured CookieStorage backend.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">storage_name</span> <span class="o">=</span> <span class="s">&#39;django.contrib.formtools.wizard.storage.cookie.CookieStorage&#39;</span>

</div>
<div class="viewcode-block" id="NamedUrlWizardView"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.NamedUrlWizardView">[docs]</a><span class="k">class</span> <span class="nc">NamedUrlWizardView</span><span class="p">(</span><span class="n">WizardView</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A WizardView with URL named steps support.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">url_name</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">done_step_name</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">get_initkwargs</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        We require a url_name to reverse URLs later. Additionally users can</span>
<span class="sd">        pass a done_step_name to change the URL name of the &quot;done&quot; view.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="s">&#39;url_name&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">,</span> <span class="s">&#39;URL name is needed to resolve correct wizard URLs&#39;</span>
        <span class="n">extra_kwargs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;done_step_name&#39;</span><span class="p">:</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;done_step_name&#39;</span><span class="p">,</span> <span class="s">&#39;done&#39;</span><span class="p">),</span>
            <span class="s">&#39;url_name&#39;</span><span class="p">:</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s">&#39;url_name&#39;</span><span class="p">),</span>
        <span class="p">}</span>
        <span class="n">initkwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">NamedUrlWizardView</span><span class="p">,</span> <span class="n">cls</span><span class="p">)</span><span class="o">.</span><span class="n">get_initkwargs</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">initkwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">extra_kwargs</span><span class="p">)</span>

        <span class="k">assert</span> <span class="n">initkwargs</span><span class="p">[</span><span class="s">&#39;done_step_name&#39;</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">initkwargs</span><span class="p">[</span><span class="s">&#39;form_list&#39;</span><span class="p">],</span> \
            <span class="s">&#39;step name &quot;</span><span class="si">%s</span><span class="s">&quot; is reserved for &quot;done&quot; view&#39;</span> <span class="o">%</span> <span class="n">initkwargs</span><span class="p">[</span><span class="s">&#39;done_step_name&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">initkwargs</span>

<div class="viewcode-block" id="NamedUrlWizardView.get_step_url"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.NamedUrlWizardView.get_step_url">[docs]</a>    <span class="k">def</span> <span class="nf">get_step_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">reverse</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url_name</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;step&#39;</span><span class="p">:</span> <span class="n">step</span><span class="p">})</span>
</div>
    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This renders the form or, if needed, does the http redirects.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">step_url</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;step&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">step_url</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="s">&#39;reset&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">first</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="p">:</span>
                <span class="n">query_string</span> <span class="o">=</span> <span class="s">&quot;?</span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">GET</span><span class="o">.</span><span class="n">urlencode</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">query_string</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
            <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_step_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">)</span>
                            <span class="o">+</span> <span class="n">query_string</span><span class="p">)</span>

        <span class="c"># is the current step the &quot;done&quot; name/view?</span>
        <span class="k">elif</span> <span class="n">step_url</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">done_step_name</span><span class="p">:</span>
            <span class="n">last_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">last</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_done</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span><span class="n">step</span><span class="o">=</span><span class="n">last_step</span><span class="p">,</span>
                <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_data</span><span class="p">(</span><span class="n">last_step</span><span class="p">),</span>
                <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">get_step_files</span><span class="p">(</span><span class="n">last_step</span><span class="p">)</span>
            <span class="p">),</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c"># is the url step name not equal to the step in the storage?</span>
        <span class="c"># if yes, change the step in the storage (if name exists)</span>
        <span class="k">elif</span> <span class="n">step_url</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">current</span><span class="p">:</span>
            <span class="c"># URL step name and storage step name are equal, render!</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span>
                <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step_data</span><span class="p">,</span>
                <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step_files</span><span class="p">,</span>
            <span class="p">),</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">elif</span> <span class="n">step_url</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">step_url</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_form</span><span class="p">(</span>
                <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step_data</span><span class="p">,</span>
                <span class="n">files</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step_files</span><span class="p">,</span>
            <span class="p">),</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="c"># invalid step name, reset to first and redirect.</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">first</span>
            <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_step_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">steps</span><span class="o">.</span><span class="n">first</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do a redirect if user presses the prev. step button. The rest of this</span>
<span class="sd">        is super&#39;d from WizardView.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">wizard_goto_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">POST</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;wizard_goto_step&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">wizard_goto_step</span> <span class="ow">and</span> <span class="n">wizard_goto_step</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_form_list</span><span class="p">():</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">render_goto_step</span><span class="p">(</span><span class="n">wizard_goto_step</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">NamedUrlWizardView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        NamedUrlWizardView provides the url_name of this wizard in the context</span>
<span class="sd">        dict `wizard`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">context</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">NamedUrlWizardView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="n">form</span><span class="o">=</span><span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">context</span><span class="p">[</span><span class="s">&#39;wizard&#39;</span><span class="p">][</span><span class="s">&#39;url_name&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">url_name</span>
        <span class="k">return</span> <span class="n">context</span>

    <span class="k">def</span> <span class="nf">render_next_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        When using the NamedUrlWizardView, we have to redirect to update the</span>
<span class="sd">        browser&#39;s URL to match the shown step.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">next_step</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_next_step</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">next_step</span>
        <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_step_url</span><span class="p">(</span><span class="n">next_step</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">render_goto_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">goto_step</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method gets called when the current step has to be changed.</span>
<span class="sd">        `goto_step` contains the requested step to go to.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">goto_step</span>
        <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_step_url</span><span class="p">(</span><span class="n">goto_step</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">render_revalidation_failure</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">failed_step</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        When a step fails, we have to redirect the user to the first failing</span>
<span class="sd">        step.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">storage</span><span class="o">.</span><span class="n">current_step</span> <span class="o">=</span> <span class="n">failed_step</span>
        <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_step_url</span><span class="p">(</span><span class="n">failed_step</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">render_done</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        When rendering the done view, we have to redirect first (if the URL</span>
<span class="sd">        name doesn&#39;t fit).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;step&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="o">!=</span> <span class="bp">self</span><span class="o">.</span><span class="n">done_step_name</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">redirect</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_step_url</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">done_step_name</span><span class="p">))</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">NamedUrlWizardView</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">render_done</span><span class="p">(</span><span class="n">form</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="NamedUrlSessionWizardView"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.NamedUrlSessionWizardView">[docs]</a><span class="k">class</span> <span class="nc">NamedUrlSessionWizardView</span><span class="p">(</span><span class="n">NamedUrlWizardView</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A NamedUrlWizardView with pre-configured SessionStorage backend.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">storage_name</span> <span class="o">=</span> <span class="s">&#39;django.contrib.formtools.wizard.storage.session.SessionStorage&#39;</span>

</div>
<div class="viewcode-block" id="NamedUrlCookieWizardView"><a class="viewcode-back" href="../../../../../ref/contrib/formtools/form-wizard.html#django.contrib.formtools.wizard.views.NamedUrlCookieWizardView">[docs]</a><span class="k">class</span> <span class="nc">NamedUrlCookieWizardView</span><span class="p">(</span><span class="n">NamedUrlWizardView</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A NamedUrlFormWizard with pre-configured CookieStorageBackend.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">storage_name</span> <span class="o">=</span> <span class="s">&#39;django.contrib.formtools.wizard.storage.cookie.CookieStorage&#39;</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../../index.html">Module code</a>
        
          <ul><li><a href="../../../../django.html">django</a>
        
        <ul><li>django.contrib.formtools.wizard.views</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>